package com.xinxing.learning.batch.service;

import com.xinxing.learning.batch.common.LogConstants;
import com.xinxing.learning.batch.entity.CdcTemp;
import com.xinxing.learning.batch.mapper.batch.CdcTempMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.batch.core.BatchStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;

@Slf4j
@Service
public class CdcTempService {
    @Autowired
    private CdcTempMapper cdcTempMapper;

    /**
     * 根据id获取cdc_temp的记录
     *
     * @param id 记录ID
     * @return {@link CdcTemp}
     */
    public CdcTemp getCurrentCdcTemp(Long id) {
        return cdcTempMapper.selectById(id);
    }

    /**
     * 根据参数更新cdc_temp表中的数据
     *
     * @param cdcTempId      记录ID
     * @param status         job状态
     * @param lastUpdateTime 最后更新时间
     */
    public void updateCdcTempAfterJod(Long cdcTempId, BatchStatus status, Date lastUpdateTime) {
        // 根据记录ID获取表中的数据
        CdcTemp cdcTemp = cdcTempMapper.selectById(cdcTempId);
        cdcTemp.setCurrentUpdateTime(new Date());
        // 正常完成则更新数据时间
        if (status == BatchStatus.COMPLETED) {
            cdcTemp.setLastUpdateTime(lastUpdateTime);
        } else {
            log.info(LogConstants.LOG_TAG + "同步状态异常：" + status.toString());
        }
        // 设置同步状态
        cdcTemp.setStatus(status.name());
        cdcTempMapper.updateById(cdcTemp);
    }
}
